package org.graduation.ical.tools;

import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Dur;
import net.fortuna.ical4j.model.component.VAlarm;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.property.*;
import net.fortuna.ical4j.util.UidGenerator;
import org.graduation.model.common.ics.ICS;

import java.net.SocketException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;


public class IcalFactory {
    public static void main(String[] args) throws ParseException, SocketException {
        IcalFactory icalFactory = new IcalFactory();
        icalFactory.test();
    }
    public static Calendar createCalendar(List<ICS> icsList){
        Calendar calendar = new Calendar();
        calendar.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 2.0//EN"));
        calendar.getProperties().add(Version.VERSION_2_0);
        calendar.getProperties().add(CalScale.GREGORIAN);
        icsList.forEach(ics -> {
            try {
                calendar.getComponents().add(createVevent(ics));
            } catch (SocketException e) {
                throw new RuntimeException(e);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        });
        return calendar;
    }
    public static VEvent createVevent(ICS ics) throws SocketException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DateTime start = new DateTime(ics.getStart());
        DateTime end = new DateTime(ics.getEnd());
        VEvent event = null;
        if (ics.isAllDay()) {
            start = new DateTime(sdf.parse(sdf.format(ics.getStart())));
            event = new VEvent(start,ics.getSummary());
        } else {
            event = new VEvent(start,end,ics.getSummary());
        }
        event.getProperties().add(new Description(ics.getDescription()));
        event.getProperties().add(new Location(ics.getLocation()));
        event.getProperties().add(new UidGenerator(ics.getUid().toString()).generateUid());
        event.getAlarms().add(createDefaultAlarm(ics));
        return event;
    }

    public static VAlarm createDefaultAlarm(ICS ics){
        VAlarm alarm = new VAlarm(new Dur(0, 0, 0, 0));
        alarm.getProperties().add(new Action("DISPLAY"));
        alarm.getProperties().add(new Repeat(1));
        alarm.getProperties().add(new Description(ics.getSummary()));
        return alarm;
    }

    public ICS testICS() throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ICS ics = new ICS();
        ics.setSummary("test ics");
        ics.setDescription("test");
        ics.setLocation("重庆");
        ics.setStart(formatter.parse("2024-02-13 13:45:00"));
        ics.setEnd(formatter.parse("2024-02-13 18:55:00"));
        ics.setZoneId(ZoneId.of("Asia/Shanghai"));
        ics.setUid(123);
        ics.setAllDay(false);
        return ics;
    }

    public void test() throws ParseException, SocketException {
        Calendar calendar = new Calendar();
        calendar.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 2.0//EN"));
        calendar.getProperties().add(Version.VERSION_2_0);
        calendar.getProperties().add(CalScale.GREGORIAN);
        calendar.getComponents().add(createVevent(testICS()));
        System.out.println(calendar);
    }
}
